#ifndef ENGINE_MEMORY_H
#define ENGINE_MEMORY_H

#include <apoget.h>

class POGET_API StorageManager
{
	StorageManager() {}
	StorageManager(const StorageManager&);
	StorageManager& operator=(const StorageManager&);
	static StorageManager theInstance;

	bool	isInitialized = false;

	static const uint MAX_STORAGE_POOL = 4;

	struct Allocator
	{
		Allocator*	next;
		size_t		length;
	};

	Allocator* freeStorage;
	Allocator* usedStorage;

	Allocator* pool[MAX_STORAGE_POOL];
	
	U8*		allocate	(size_t size, size_t alignment = 4);
	void	deallocate	(void* p);
	U8*		getStorage	(size_t& storageLength);
	void	dequeueFreeStorage(Allocator& allocator);
	Allocator* mergeAllocators(Allocator& first, Allocator& second);

public:
	static StorageManager& getInstance();
	bool	initialize();
	bool	shutdown();

	void*	New			(size_t size_bytes, 
							size_t alignment = 4);
	void	Delete		(void * pMem);

};
#define gStorageManager StorageManager::getInstance()

#endif